# /bin/bash
#
#

# shell full path
#
#
g_app_full_path=$(readlink -m $0)                               ;   # current application absolute file path
g_app_root_dir=$(dirname $g_app_full_path)                      ;   # current app;ication current root directory path
g_virtual_terminal_socket_name="virtual.terminal.medation.x"    ;   # virtual terminal socket name
g_redis_split_number=2                                          ;   # redis master list split max number 


# append virtual terminal log
#
#
function log_x()
{
    local v_log_name="";
    local v_log_content="$1";
    local v_virtual_terminal_log_file="$g_app_root_dir/virtual.terminal.log"; echo -e "$(get_current_date) $v_log_content" >> $v_virtual_terminal_log_file;
}
function get_current_date()
{
    echo "[$(date)]";
}
# helper information
#
#
function server_help()
{
    echo "description : restart a group of backend services process in the virtual terminal";
    echo "developer   : BREEZEER"   ;
    echo "release     : 2023.09.26" ;
    echo "version     : 1.0.0.4"    ;
    echo -e "\e[38;5;47musage\e[0m       : $g_app_full_path [help|stop|restart|kill.all.log|status]";
}

# application entrance 
#
#
function main()
{
    local v_cmd=$1
    
    #
    #
    case "$v_cmd" in
        ""|"--version"|"h"|"-h"|"--help"|"help")
             
            server_help;
            ;;
        "stop")
             
            server_stop;
            ;;
       "restart")

            server_stop; 
            server_restart;
            ;;
        "start_in_the_virtual_terminal")

            service_start;
            ;; 

        "status")

            server_status;
            ;; 
        "kill.all.log")
            
            server_kill_all_log;
           ;; 
    esac;
}
function server_status()
{
    ps uax|grep -iE \("import.excel.file.data.2.redis.list.for.linux.x64"\|"artisan command:importExcelMe"\|"go.test.x"\|"artisan c_split_redis_list_2_x"\|"artisan sendLawZhiZhiMeLatter --split-number=[0-9]+"\|"artisan sendLawMeLatter --split-number=[0-9]+"\)|grep -ivE \("grep"\|"tail -f "\) |nl -n rz;
}
function server_kill_all_log()
{
    # remove all log file
    #
    #
    $(  
        cd $g_app_root_dir;
        \rm ./import.excel.file.data.2.redis.list.for.linux.x64.log  2>/dev/null; 
        \rm ./go.test.x.log                                          2>/dev/null; 
        \rm ./php.artisan.command.import.exxcel.me.log               2>/dev/null; 
        \rm ./php.artisan.command.c_split_redis_list_2_x.log         2>/dev/null; 
        \rm ./virtual.terminal.log                                   2>/dev/null; 
        \rm ./php.artisan.command.sendLawZhiZhiMeLatter.*.log        2>/dev/null;
        \rm ./php.artisan.command.sendLawMeLatter.*.log              2>/dev/null;
    ) ;
}

# start backend service in the virtual terminal
#
#
#
function server_restart()
{
    echo "service restart ,please waiting for ...";

    # start virtual terminal process and detach 
    #
    #
    local v_virtual_terminal_pid=$( screen -ls|grep -i $g_virtual_terminal_socket_name|awk -F '.' '{print $1}'|sed 's/\s//g';);if [[ "" == "$v_virtual_terminal_pid" ]]; then screen -dmS $g_virtual_terminal_socket_name; else :; fi;

    # push backend process to this virtual terminal
    #
    #
    local cmd="screen -S $g_virtual_terminal_socket_name -X screen /bin/bash $g_app_full_path start_in_the_virtual_terminal ;"; eval $cmd; 

    #
    #
    #
    sleep 3;server_status;
}

# start services
#
#
#
function service_start()
{
    log_x "service start ...";

    # start import execel file data to redis
    #
    #
    log_x "start import execel file data to redis";
    local v_app_for_import_excel_file_data_2_redis_list_for_linux_x64="$g_app_root_dir/import.excel.file.data.2.redis.list.for.linux.x64"
    chmod 777 $v_app_for_import_excel_file_data_2_redis_list_for_linux_x64; $v_app_for_import_excel_file_data_2_redis_list_for_linux_x64 --aes_key "0123456789abcdef" --http_server --redis_list_key zlh_test --http_server_port 39350 2>&1 1>"${v_app_for_import_excel_file_data_2_redis_list_for_linux_x64}.log"  &

    # start web socket
    #
    #
    log_x "start web socket "; local v_app_for_web_socket="$g_app_root_dir/go.test.x"; chmod 777 $v_app_for_web_socket ; $v_app_for_web_socket 2>&1 1>"${v_app_for_web_socket}.log" & 

    # start php artisman        => c_split_redis_list_2_x [redis.key: mediation_center_key_send_law_zz_letter_list]
    #
    #
    {
        log_x "start php artisan c_split_redis_list_2_x => redis.key : mediation_center_key_send_law_zz_letter_list";
        $(
           local v_app_for_php_artisan_command_log="$g_app_root_dir/php.artisan.command.c_split_redis_list_2_x.log";
           cd $g_app_root_dir/../../..;/www/server/php/80/bin/php artisan c_split_redis_list_2_x --redis-list-key-name=mediation_center_key_send_law_zz_letter_list --split-number=$g_redis_split_number --confirm-tips=n   2>&1 1>  $v_app_for_php_artisan_command_log      ;
        ); 
    }&

    # start php artisman        => c_split_redis_list_2_x [redis.key : mediation_center_key_send_law_letter_list]
    #
    #
    {
        log_x "start php artisan c_split_redis_list_2_x => redis.key : mediation_center_key_send_law_letter_list";
        $(
           local v_app_for_php_artisan_command_log="$g_app_root_dir/php.artisan.command.c_split_redis_list_2_x.log";
           cd $g_app_root_dir/../../..;/www/server/php/80/bin/php artisan c_split_redis_list_2_x --redis-list-key-name=mediation_center_key_send_law_letter_list --split-number=$g_redis_split_number --confirm-tips=n      2>&1 1>> $v_app_for_php_artisan_command_log      ;
        ); 
    }&








    # start php artisman        => sendLawZhiZhiMeLatter
    #
    #
    #
    #
    #
    #
    #
    #
    #
    #
    #
    { service_start_for_php_artisan_at_sendLawZhiZhiMeLatter_by_process_X 0; }&
    { service_start_for_php_artisan_at_sendLawZhiZhiMeLatter_by_process_X 1; }&
    #
    #
    #
    #
    #
    #
    #
    #
    #
    # start php artisman        => sendLawZhiZhiMeLatter end

    # start php artisman        => sendLawMeLatter
    #
    #
    #
    #
    #
    #
    #
    #
    #
    #
    #
    { service_start_for_php_artisan_at_sendLawMeLatter_by_process_X 0; }&
    { service_start_for_php_artisan_at_sendLawMeLatter_by_process_X 1; }&
    #
    #
    #
    #
    #
    #
    #
    #
    #
    # start php artisman        => sendLawMeLatter end








    # start php artisman        => command:importExcelMe
    #
    #
    {
        log_x "start php artisan command:importExcelMe";
        $(
            local v_app_for_php_artisan_command_log="$g_app_root_dir/php.artisan.command.import.exxcel.me.log";
            cd $g_app_root_dir/../../..;/www/server/php/80/bin/php artisan command:importExcelMe                                                                                                                            2>&1 1> $v_app_for_php_artisan_command_log      ;
        );
    }
}

function service_start_for_php_artisan_at_sendLawMeLatter_by_process_X()
{
    local index=$1;
    log_x "start php artisan sendLawMeLatter --split-number=$index";
    $(
       local v_app_for_php_artisan_command_log="$g_app_root_dir/php.artisan.command.sendLawMeLatter.$index.log";
       cd $g_app_root_dir/../../..;/www/server/php/80/bin/php artisan sendLawMeLatter --split-number=$index                                                                                                                 2>&1 1> $v_app_for_php_artisan_command_log      ;
    );
}

function service_start_for_php_artisan_at_sendLawZhiZhiMeLatter_by_process_X()
{
    local index=$1;
    log_x "start php artisan sendLawZhiZhiMeLatter --split-number=$index";
    $(
       local v_app_for_php_artisan_command_log="$g_app_root_dir/php.artisan.command.sendLawZhiZhiMeLatter.$index.log";
       cd $g_app_root_dir/../../..;/www/server/php/80/bin/php artisan sendLawZhiZhiMeLatter --split-number=$index                                                                                                             2>&1 1> $v_app_for_php_artisan_command_log      ;
    );
}
function server_stop()
{
    # virtual terminal log
    #
    #
    log_x "\e[38;5;116mserver stop start ... \e[0m";

    # kill go process
    #
    #
    killall import.excel.file.data.2.redis.list.for.linux.x64 2>/dev/null;killall go.test.x 2>/dev/null;log_x "kill go processes which included [ import.excel.file.data.2.redis.list.for.linux.x64,go.test.x ]";

    # kill php artisan
    #
    #
    #
    {
        # kill 0
        #
        #
        local v_php_artisan_pids=$( ps aux|grep -i "/www/server/php/80/bin/php artisan command:importExcelMe"                   |grep -iv grep|awk -F ' ' '{print $2}';);if [[ "" == "$v_php_artisan_pids" ]];then :; else kill $v_php_artisan_pids;fi; log_x "kill php artisan command:importExcelMe"                  ;
        
        # kill 1.0
        #
        #
        local v_php_artisan_pids=$( ps aux|grep -i "/www/server/php/80/bin/php artisan c_split_redis_list_2_x --redis-list-key-name=mediation_center_key_send_law_zz_letter_list"|grep -iv grep|awk -F ' ' '{print $2}';);if [[ "" == "$v_php_artisan_pids" ]];
        then 
            :; 
        else 
            kill $v_php_artisan_pids;
        fi;
        log_x "kill php artisan c_split_redis_list_2_x => redis.key : mediation_center_key_send_law_zz_letter_list";

        # kill 1.1
        #
        #
        local v_php_artisan_pids=$( ps aux|grep -i "/www/server/php/80/bin/php artisan c_split_redis_list_2_x --redis-list-key-name=mediation_center_key_send_law_letter_list"|grep -iv grep|awk -F ' ' '{print $2}';);if [[ "" == "$v_php_artisan_pids" ]];
        then 
            :; 
        else 
            kill $v_php_artisan_pids;
        fi;
        log_x "kill php artisan c_split_redis_list_2_x => redis.key : mediation_center_key_send_law_letter_list";

        # kill 2.0
        #
        #
        local v_php_artisan_pids=$( ps aux|grep -i "/www/server/php/80/bin/php artisan sendLawZhiZhiMeLatter --split-number=0"  |grep -iv grep|awk -F ' ' '{print $2}';);if [[ "" == "$v_php_artisan_pids" ]];then :; else kill $v_php_artisan_pids;fi; log_x "kill php artisan sendLawZhiZhiMeLatter --split-number=0" ;

        # kill 2.1
        #
        #
        local v_php_artisan_pids=$( ps aux|grep -i "/www/server/php/80/bin/php artisan sendLawZhiZhiMeLatter --split-number=1"  |grep -iv grep|awk -F ' ' '{print $2}';);if [[ "" == "$v_php_artisan_pids" ]];then :; else kill $v_php_artisan_pids;fi; log_x "kill php artisan sendLawZhiZhiMeLatter --split-number=1" ;

        # kill 3.0
        #
        #
        local v_php_artisan_pids=$( ps aux|grep -i "/www/server/php/80/bin/php artisan sendLawMeLatter --split-number=0"        |grep -iv grep|awk -F ' ' '{print $2}';);if [[ "" == "$v_php_artisan_pids" ]];then :; else kill $v_php_artisan_pids;fi; log_x "kill php artisan sendLawMeLatter --split-number=0" ;

        # kill 3.1
        #
        #
        local v_php_artisan_pids=$( ps aux|grep -i "/www/server/php/80/bin/php artisan sendLawMeLatter --split-number=1"        |grep -iv grep|awk -F ' ' '{print $2}';);if [[ "" == "$v_php_artisan_pids" ]];then :; else kill $v_php_artisan_pids;fi; log_x "kill php artisan sendLawMeLatter --split-number=1" ;
    };

    # kill screen session
    #
    #
    local v_virtual_terminal_pid=$( screen -ls|grep -i $g_virtual_terminal_socket_name|awk -F '.' '{print $1}'|sed 's/\s//g';);if [[ "" == "$v_virtual_terminal_pid" ]];then :; else kill $v_virtual_terminal_pid;fi;log_x "kill screen session";

    #
    #
    #
    log_x "server stop end";
}

# go ...
#
#
main $*;
